package com.powerwyx.shortlink.admin.controller;

import com.powerwyx.shortlink.admin.common.convention.result.Result;
import com.powerwyx.shortlink.admin.common.convention.result.Results;
import com.powerwyx.shortlink.admin.pojo.dto.UserLoginDTO;
import com.powerwyx.shortlink.admin.pojo.dto.UserRegisterDTO;
import com.powerwyx.shortlink.admin.pojo.dto.UserUpdateDTO;
import com.powerwyx.shortlink.admin.pojo.vo.UserLoginVO;
import com.powerwyx.shortlink.admin.pojo.vo.UserVO;
import com.powerwyx.shortlink.admin.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 王艺锡
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/shortlink/admin/v1/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 根据用户名查询用户信息
     */
    @GetMapping("/{username}")
    public Result<UserVO> getUserByUserName(@PathVariable("username") String username){
        UserVO userVO = userService.getUserByUserName(username);
        return Results.success(userVO);
    }


    /**
     * 查询用户名是否存在
     */
    @GetMapping("/hasUserName")
    public Result<Boolean> hasUserName(String username){
        return Results.success(userService.hasUserName(username));
    }


    /**
     * 注册用户
     *
     */
    @PostMapping("/register")
    public Result<Void> userRegister(@RequestBody UserRegisterDTO userRegisterDTO){
        userService.register(userRegisterDTO);
        return Results.success();
    }


    /**
     * 修改用户信息
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/updateUserInfo")
    public Result<Void> updateUserInfo(@RequestBody UserUpdateDTO userUpdateDTO){
        userService.updateUserInfo(userUpdateDTO);
        return Results.success();
    }

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        return Results.success(userLoginVO);
    }


    /**
     * 检查用户是否登录
     * @param username
     * @param token
     * @return
     */
    @GetMapping("/checkUserLogin")
    public Result<Boolean> checkUserLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkUserLogin(username,token));
    }




    /**
     * 用户退出
     * @param username
     * @param token
     * @return
     */
    @PostMapping("/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }



}


